﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ViewModels;

namespace DAL
{
    /// <summary>
    /// 借阅数据访问类
    /// </summary>
    public class BorrowDAL
    {
        SqlCommand cmd;
        public BorrowDAL()
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;//定义Command对象执行的是存储过程
        }
        /// <summary>
        /// 根据ID获取用户相片
        /// </summary>
        /// <param name="stuid">用户ID</param>
        /// <returns>MemoryStream数据流</returns>
        public MemoryStream GetUserPhoto(Guid stuid)
        {
            cmd.CommandText = "GetUserPhotoByID";//定义存储过程的名字
            cmd.Parameters.Clear();//清空参数
            cmd.Parameters.Add("@stuid", SqlDbType.UniqueIdentifier).Value = stuid;
            MemoryStream obj = SQLHelper.GetPhotoByID(cmd);
            return obj;
        }

        public DataSet GetUserBorrowListByID(Guid stuid)
        {
            cmd.CommandText = "GetUserBorrowListByID";//定义存储过程的名字
            cmd.Parameters.Clear();//清空参数
            cmd.Parameters.Add("@stuid", SqlDbType.UniqueIdentifier).Value = stuid;
            DataSet ds = new DataSet();
            ds = SQLHelper.GetDataSet(cmd);
            return ds;
        }
        public UserVM GetUserInfoByStuNo(string stuno)
        {
            UserVM vm=new UserVM();

            cmd.CommandText = "GetUserInfoByStuNo";//定义存储过程的名字
            cmd.Parameters.Clear();//清空参数
            cmd.Parameters.Add("@stuno", SqlDbType.Char, 11).Value = stuno;
            SqlDataReader sdr = SQLHelper.ExcuteReader(cmd);
            while (sdr.Read())
            {
                vm = new UserVM()
                {
                    UserID = Guid.Parse(sdr["UserId"].ToString()),
                    StuNo = sdr["StuNo"].ToString(),
                    Name = sdr["Name"].ToString(),
                    Gender = bool.Parse(sdr["Gender"].ToString()),
                    Mobile = sdr["Mobile"].ToString(),
                    URL = sdr["URL"].ToString(),
                    ClassName = sdr["ClassName"].ToString()
                };
            }
            return vm;
        }

        public bool GetBookState(string bookid)
        {
            try
            {
                cmd.CommandText = "GetBookState";//定义存储过程的名字
                cmd.Parameters.Clear();//清空参数
                cmd.Parameters.Add("@bookid", SqlDbType.VarChar,20).Value = bookid;
                cmd.Parameters.Add("@returnVal", SqlDbType.Bit).Direction = ParameterDirection.Output;//存储过程回传
                SQLHelper.ExcuteSQL(cmd);
                var result = cmd.Parameters["@returnVal"].Value.ToString();
                return bool.Parse(result);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
        public bool InsertBorrowList(string bookid,Guid userid)
        {
            //待完成
            bool result = false;
            return result;
        }
    }
}
